package com.ling.pl.security.controller;

import com.ling.pl.security.model.*;
import com.ling.pl.security.service.DefaultPositionService;
import com.ling.pl.security.service.DefaultUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author bo.wang
 */
@RequestMapping("/defaultOrg")
@Api(value = "defaultOrg", description = "API")
@RestController("defaultOrgController")
public class DefaultOrgController extends BaseDefaultOrgController {

    Log log = LogFactory.getLog(DefaultOrgController.class);

    @Resource
    DefaultPositionService positionService;
    @Resource
    DefaultUserService userService;

    @ApiOperation(value = "loadOrgs")
    @RequestMapping(value = "/loadOrgs", method = RequestMethod.GET)
    public List<DefaultOrg> loadOrgs(@ApiParam(value = "parentId") @PathVariable(name = "parentId") String parentId) throws Exception {
        List result = defaultOrgService.loadOrgsByParentId(parentId, null, new HashMap());
        return result;
    }

    @ApiOperation(value = "uniqueCheck")
    @RequestMapping(value = "/uniqueCheck", method = RequestMethod.GET)
    public String uniqueCheck(@ApiParam(value = "id") @PathVariable(name = "id") String id) {
        return defaultOrgService.uniqueCheck(id);
    }

    @ApiOperation(value = "countChildren")
    @RequestMapping(value = "/countChildren", method = RequestMethod.GET)
    public int countChildren(@ApiParam(value = "parentId") @PathVariable(name = "parentId") String parentId) {
        return defaultOrgService.countChildren(parentId);
    }

    @ApiOperation(value = "loadPositions")
    @RequestMapping(value = "/loadPositions", method = RequestMethod.GET)
    public List<DefaultPositionVO> loadPositions(@ApiParam(value = "orgId") @PathVariable(name = "orgId") String orgId) {
        List<DefaultPosition> positions = defaultOrgService.loadPositionsByOrgId(orgId);
        return positionService.convertDefaultPositionToVO(positions);
    }

    @ApiOperation(value = "loadUsers")
    @RequestMapping(value = "/loadUsers", method = RequestMethod.GET)
    public List<DefaultUserVO> loadUsers(@ApiParam(value = "orgId") @PathVariable(name = "orgId") String orgId) {
        List<DefaultUser> users = defaultOrgService.loadUsersByOrgId(orgId);
        return userService.convertDefaultUserToVO(users);
    }
}

